package com.example.zhanglinan.vrdemo.activity;

import android.graphics.Bitmap;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

import com.example.zhanglinan.vrdemo.R;
import com.google.vr.sdk.widgets.pano.VrPanoramaView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.BitmapCallback;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

public class PanoActivity extends AppCompatActivity {

    private VrPanoramaView vrPanoView;
    private ActionBar actionBar;
    private String url;
    private String name;
    private String mp3;
    private MediaPlayer player;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pano);
        init();
    }

    private void init() {
        initView();
        initActionBar();
        initData();
    }

    private void initData() {
        final VrPanoramaView.Options options = new VrPanoramaView.Options();
        options.inputType = VrPanoramaView.Options.TYPE_MONO;
        OkGo.get(url)
                .tag(this)
                .execute(new BitmapCallback() {
                    @Override
                    public void onSuccess(Bitmap bitmap, Call call, Response response) {
                        vrPanoView.loadImageFromBitmap(bitmap, options);
                    }
                });
    }

    private void initActionBar() {
        actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initView() {
        vrPanoView = (VrPanoramaView) findViewById(R.id.vr_pano);
        vrPanoView.setInfoButtonEnabled(false);
        url = getIntent().getStringExtra("url");
        name = getIntent().getStringExtra("name");
        mp3 = getIntent().getStringExtra("mp3");
        if (mp3 != null) {
            initPlayer(mp3);
        }
    }

    private void initPlayer(String mp3) {
        player = new MediaPlayer();
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);

        try {
            player.setDataSource(this, Uri.parse(mp3));
            player.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        vrPanoView.pauseRendering();
        if (player != null) {
            player.pause();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        vrPanoView.resumeRendering();
        if (player != null) {
            player.start();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkGo.getInstance().cancelTag(this);
        vrPanoView.shutdown();
        if (player != null) {
            player.stop();
            player.release();
            player = null;
        }
    }
}
